<?php
namespace app\admin\controller;

use app\model\admin;
use think\Request;
use think\Session;
use think\Controller;
use think\File;

class Column extends Controller
{
    //栏目列表
    public function index()
    {
        $dao = new \app\model\admin\Column();
        $where=[];
        $lists = $dao->get_column_lists($where,$sort='id desc',$paginate=10);
        $this->assign('lists', $lists);
        return $this->fetch('column/index');
    }

    //栏目详情
    public function detail()
    {
        $dao = new \app\model\admin\Column();
        $id = $request = $this->request->get('id');
        $c_dao = new \app\model\admin\Column();
        $c_where['status']=1;
        $c_where['c_id']=0;
        $c_lists = $c_dao->get_column_lists($c_where,$sort='id desc',$paginate=20);//查询栏目
        if(empty($id)){
            $data = array();
        }else{
            $data = $dao->column_detail($id);
        }
        $this->assign('c_lists', $c_lists);
        return $this->fetch('column/detail',$data);
    }

    //栏目保存修改
    public function column_save()
    {
        $dao = new \app\model\admin\Column();
        $data['id'] = deletehtml($this->request->post('id'));
        $data['name'] = deletehtml($this->request->post('name'));
        $data['c_id'] = deletehtml($this->request->post('column'));
        $data['keywords'] = deletehtml($this->request->post('keywords'));
        $data['describe'] = deletehtml($this->request->post('describe'));
        $data['type'] = deletehtml($this->request->post('type'));
        $data['seo_name_en'] = deletehtml($this->request->post('seo_name_en'));
        $file = $this->request->file('image');
        if(!empty($file)){//如果图片不为空则为修改
            $updimg_status = upload_file($file,'jpg,png,gif','512000','/uploads/column_img');
            if($updimg_status){ //如果图片上传成功，则把图片路径写到数据库里
                $data['image'] = $updimg_status;
            }
        }
        if(empty($data['id'])){
            $colimn_status = $dao->add_column($data);
        }else{
            $where['id'] = $data['id'];
            $colimn_status = $dao->update_column($where,$data);
        }
        if($colimn_status){
            $this->redirect('/admin/column/index?');
        }else{
            $this->error('操作失败！', '/admin/column/detail?id='.$data['id']);
        }
    }

    //栏目删除
    public function column_delete()
    {
        $dao = new \app\model\admin\Column();
        if($this->request->get("id")<1){
            return json(['status'=>2,'data'=>(object)[],'msg'=>'参数错误!']);
        }else{
            $id = $this->request->get("id");
        }
        $column_delete = $dao->delete_column([$id]);
        if($column_delete == 0){
            return json(['status'=>3,'data'=>(object)[],'msg'=>'删除失败!']);
        }else{
            return json(['status'=>0,'data'=>$column_delete,'msg'=>'操作成功!']);
        }
    }

    //栏目列表
    public function search_index()
    {
        $dao = new \app\model\admin\Column();
        $where=[];
        $cid = (int)$this->request->get('c_id');
        if($cid){
            $where['c_id'] = $cid;
        }
        $lists = $dao->get_column_select($where,$sort='id desc');
        if(!empty($lists))return json(['status'=>0,'data'=>$lists,'msg'=>'ok!']);
        else return json(['status'=>1001,'data'=>[],'msg'=>'ok!']);
    }

}


